package com.jie.lipassword;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jie.lipassword.db.DbContect;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class AccountPasswordActivity extends AppCompatActivity {
    private RelativeLayout account_password_title;
    private EditText account_user;
    private EditText account_password;
    private EditText account_note;
    private ImageView account_user_copy;
    private ImageView account_password_copy;
    private ImageButton account_edit_save;
    private ImageButton account_delete;


    private String type_name;
    private String account;
    private DbContect helper;
    private SQLiteDatabase db;
    private String temp_account;
    private String temp_note;
    private String temp_password;
    private Boolean is_edit_save = false;
    private ClipboardManager clipboard;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_password);
        type_name = getIntent().getStringExtra("type_name");
        account = getIntent().getStringExtra("account");

        dbcontext();
    }

    @Override
    protected void onResume() {
        super.onResume();
        set_init();
        showaccount();
    }

    @SuppressLint("Range")
    private void showaccount() {
        Cursor cursor = db.query("PasswordParameters",null,"type_name=? and account=?",new String[]{type_name,account},null,null,null);
        while(cursor.moveToNext()){
            temp_account = cursor.getString(cursor.getColumnIndex("account"));
            temp_password = cursor.getString(cursor.getColumnIndex("password"));
            temp_note = cursor.getString(cursor.getColumnIndex("note"));
        }
        // 关闭游标，释放资源
        cursor.close();

        account_user.setText(temp_account);
        account_password.setText(temp_password);
        account_note.setText(temp_note);

    }

    private void dbcontext(){
        helper = new DbContect(this);
        db = helper.getWritableDatabase();
    }
    private void set_init() {
        //标题文本框
        account_password_title = findViewById(R.id.account_password_title);
        //账号文本框
        account_user = findViewById(R.id.account_user);
        //密码文本框
        account_password = findViewById(R.id.account_password);
        //备注文本框
        account_note = findViewById(R.id.account_note);
        //账号复制按钮
        account_user_copy = findViewById(R.id.account_user_copy);
        //密码复制按钮
        account_password_copy = findViewById(R.id.account_password_copy);
        //编辑保存按钮
        account_edit_save = findViewById(R.id.account_edit_save);
        //删除按钮
        account_delete = findViewById(R.id.account_delete);

        //剪贴板
        clipboard = (ClipboardManager) getSystemService(this.CLIPBOARD_SERVICE);


        //标题设置方法
        FindTitle.setTitle(AccountPasswordActivity.this,account_password_title,true,false,type_name);
        //账号复制点击事件
        account_user_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipData clip = ClipData.newPlainText("label", account_user.getText().toString());
                clipboard.setPrimaryClip(clip);
                Toast.makeText(AccountPasswordActivity.this,"已复制到剪贴板", Toast.LENGTH_SHORT).show();

            }
        });
        //密码复制按钮
        account_password_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipData clip = ClipData.newPlainText("label", account_password.getText().toString());
                clipboard.setPrimaryClip(clip);
                Toast.makeText(AccountPasswordActivity.this,"已复制到剪贴板", Toast.LENGTH_SHORT).show();
            }
        });

        //编辑保存事件
        account_edit_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!is_edit_save){
                    is_edit_save = !is_edit_save;
                    account_edit_save.setImageResource(R.drawable.save_icon);
//                    account_user.setEnabled(true);
                    account_password.setEnabled(true);
                    account_note.setEnabled(true);
                }else{
                    // 创建AlertDialog.Builder对象
                    AlertDialog.Builder builder = new AlertDialog.Builder(AccountPasswordActivity.this);
                    builder.setTitle("提示"); // 设置弹窗标题
                    builder.setMessage("是否修改"); // 设置弹窗内容
                    builder.setIcon(R.drawable.edit_icon);

                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //点击确定按钮后的操作
                            //判断密码是否为空
                            if (!account_password.getText().toString().equals("")){
                                ContentValues values = new ContentValues();
                                values.put("password", account_password.getText().toString());
                                values.put("note", account_note.getText().toString());
                                db.update("PasswordParameters",values,"type_name=? and account=?",new String[]{type_name,account_user.getText().toString()});
                                dialog.dismiss(); // 关闭弹窗
                                finish();
                            }else {
                                //为空
                                Log.i("TAG", "空 ");
                                dialog.dismiss(); // 关闭弹窗
                                finish();
                            }
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 点击取消按钮后的操作
                            dialog.dismiss(); // 关闭弹窗
                        }
                    });

                    // 创建并显示AlertDialog
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
        //删除点击事件
        account_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AccountPasswordActivity.this);
                builder.setTitle("提示"); // 设置弹窗标题
                builder.setMessage("是否删除"); // 设置弹窗内容

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击确定按钮后的操作
                        db.delete("PasswordParameters","type_name=? and account=?",new String[]{type_name,account_user.getText().toString()});
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击取消按钮后的操作
                        dialog.dismiss(); // 关闭弹窗
                    }
                });

                // 创建并显示AlertDialog
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }
}
